package com.livesource.client.webservices.subversion;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.livesource.client.LiveSourceGlobalVariables;
import com.livesource.client.model.entry.ProjectClass;
import com.livesource.client.model.project.Project;
import com.livesource.client.web.repositoryLogin.EncryptText;
import com.livesource.client.web.repositoryLogin.GetCookies;
import com.livesource.client.web.sourceCode.viewOptions.ButtonFeatureDescription;
import com.livesource.client.webservices.GetFileContent;
import com.livesource.client.webservices.RequestSaveLiveFeed;

public class RequestSaveClassDescription {

	// http://livesourceagile.com:8080/LiveSourceWebServices/SaveSubversionClassDescriptionService
	// ?repositoryURL=https://tictactoe4j.googlecode.com/svn/trunk
	// &username=alline.oliveira&password=bD8Km8se2rV9
	// &filePath=src/tictactoe/authentication/client/Login.java
	// &classDescription=Form%20to%20identify%20the%20name%20and%20the%20password%20of%20the%20player.
	// &classStatus=ToDo&classType=Feature

	private static String url = "http://livesourceagile.com:8080/LiveSourceWebServices/SaveSubversionClassDescriptionService";

	public static void sendPostRequest(Project project, String filePath,
			final ProjectClass classFileUpdated) {

		String username = EncryptText.decrypt(GetCookies.getUsername(project
				.getProjectID()));

		if (username != null) {

			String password = EncryptText.decrypt(GetCookies
					.getPassword(project.getProjectID()));

			String urlSave = url + "?repositoryURL="
					+ project.getRepositoryURL() + "&username=" + username
					+ "&password=" + password;

			urlSave += "&filePath=" + filePath;

			String status = "";
			if (classFileUpdated.getStatus() != null) {

				status = classFileUpdated.getStatus();
			}

			String description = "";
			if (classFileUpdated.getFeatureDescription() != null) {

				description = classFileUpdated.getFeatureDescription();
			}

			String type = "";
			if (classFileUpdated.getFeatureType() != null) {

				type = classFileUpdated.getFeatureType();
			}

			String featureInfo = "&classDescription=" + description
					+ "&classStatus=" + status + "&classType=" + type;

			RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
					urlSave + featureInfo);

			// builder.setRequestData(requestData);

			try {
				Request request = builder.sendRequest(null,
						new RequestCallback() {

							public void onError(Request request,
									Throwable exception) {

								System.out.println("Couldn't retrieve JSON");
							}

							public void onResponseReceived(Request request,
									Response response) {

								new GetFileContent(
										classFileUpdated,
										LiveSourceGlobalVariables.currentProject);

								if (200 == response.getStatusCode()) {

								} else {
									// displayError("Couldn't retrieve JSON ("
									// + response.getStatusText() + ")");
								}
							}
						});

			} catch (RequestException e) {
				// displayError("Couldn't retrieve JSON");
			}

			RequestSaveLiveFeed.feedUpdateTask(
					LiveSourceGlobalVariables.currentUserID, classFileUpdated);

			LiveSourceGlobalVariables.projectEntries.put(
					classFileUpdated.toString(), classFileUpdated);

			ButtonFeatureDescription.showDescription(classFileUpdated);
		}
	}
}